package com.ketai.dao.impl;

import com.ketai.dao.BaseDao;
import com.ketai.dao.ConsulDao;
import com.ketai.pojo.CUser;
import com.ketai.pojo.Consul;

import javax.naming.NamingException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:胡远博
 * @Description:
 * @Date:Create In 2022-08-11 11:24
 **/
public class ConsulDaoImpl extends BaseDao implements ConsulDao {
    @Override
    public List<Consul> findAll(String sql, Object[] params) throws SQLException, NamingException {
        List<Consul> list = new ArrayList<Consul>();
        ResultSet rs = query(sql, params);
        while (rs.next()) {
            Consul consul = new Consul();
            consul.setId(rs.getString("id"));
            consul.setTitle(rs.getString("title"));
            consul.setDetaic(rs.getString("detaic"));
            consul.setTime(rs.getString("time"));
            consul.setCreated(rs.getString("created"));
            list.add(consul);
        }
        return list;
    }

    @Override
    public long getTotalCUserCount(String sql, Object[] params) throws SQLException, NamingException {
        ResultSet rs = query(sql, params);
        while (rs.next()) {
            return rs.getLong("total");
        }

        return 0;
    }

    @Override
    public int deleteById(String id) throws SQLException, NamingException {
        String sql = "delete from consul where id = " + id;
        return curdMethod(sql, null);
    }

    @Override
    public int insertConsul(Consul consul) throws SQLException, NamingException {
        String sql = "insert into consul values(?,?,?,?,?)";
        Object[] params = {consul.getId(),consul.getTitle(),consul.getDetaic(),consul.getTime(),consul.getCreated()};
        return curdMethod(sql, params);
    }
}
